package com.dongxing.concyrrency.chapter12;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author XavierDong
 **/
public class ThreadGroupAPI {

    public static void main(String[] args) {
        // use the name
        ThreadGroup tg1 = new ThreadGroup("TG1");
        Thread t1 = new Thread(tg1, "t1") {
            @Override
            public void run() {
                while (true) {
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t1.start();

        ThreadGroup tg2 = new ThreadGroup(tg1, "TG2");
        Thread t2 = new Thread(tg2, "T2") {

            @Override
            public void run() {

                while (true) {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t2.start();


        System.out.println(tg1.activeCount());
        System.out.println(tg1.activeGroupCount());
        t2.checkAccess();

        // 必须是为空的，才能destroy()
//        tg1.destroy();

        System.out.println("===========================");
        Thread[] ts1 = new Thread[tg1.activeCount()];
        tg1.enumerate(ts1);
        Arrays.asList(ts1).forEach(System.out::println);

        System.out.println("===========================");
        tg1.enumerate(ts1,false);
        tg1.enumerate(ts1, false);

    }
}
